package systemBeat;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import system.KaoChang;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class SheetCreate {
    private XSSFWorkbook wb;
    private FileInputStream fileInputStream;
    private String filePath;
    private boolean isFirst=true;
    private XSSFSheet sheet;
    private KaoChang kaoChang;

    public SheetCreate(String filePath,KaoChang kaoChang){
        this.filePath=filePath;
        this.kaoChang=kaoChang;
        File file=new File(filePath);
        if(!this.isFirst&&file.exists()){
            try {
                fileInputStream=new FileInputStream(file);
                this.wb=new XSSFWorkbook(fileInputStream);
            } catch (IOException e) {
                e.printStackTrace();
                this.wb=new XSSFWorkbook();
            }
        }else{
            wb=new XSSFWorkbook();
            this.isFirst=false;
        }
    }

    public void setTitle(String title,int cols){
        sheet.addMergedRegion(new CellRangeAddress(0,0,0,cols));
        XSSFRow row=sheet.createRow(0);
        row.setHeight((short) (39.75*20));
        XSSFCell cell=row.createCell(0);
        cell.setCellValue(title);
        cell.setCellStyle(getCellStyle(true,true,false,Const.titlePointNumber));
    }

    /**
     * 设置标题的CellStyle
     * @return
     */
    public XSSFCellStyle getCellStyle(boolean createBorder,boolean wrapText,boolean isNumber,short pointNumber){
        XSSFCellStyle cellStyle=wb.createCellStyle();
        cellStyle.setVerticalAlignment(cellStyle.getVerticalAlignmentEnum().CENTER);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        if(createBorder){
            cellStyle.setBorderTop(BorderStyle.THIN);//上边框
            cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
            cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
            cellStyle.setBorderRight(BorderStyle.THIN);//右边框
        }
        if(wrapText){
            cellStyle.setWrapText(true);
        }
        Font font = wb.createFont();
        font.setBold(true);
        font.setFontHeightInPoints(pointNumber);
        if(isNumber){
            font.setFontName("Tahoma");
        }else{
            font.setFontName("宋体");
        }
        cellStyle.setFont(font);
        return cellStyle;
    }

//    public XSSFCellStyle getKaohaoCellstyle(){
//        XSSFCellStyle cellStyle = wb.createCellStyle();
//
//        cellStyle.setVerticalAlignment(cellStyle.getVerticalAlignmentEnum().CENTER);
//        cellStyle.setAlignment(HorizontalAlignment.CENTER);
//        Font font = wb.createFont();
//        font.setBold(true);
//        font.setFontHeightInPoints((short) 11);
//        font.setFontName("Tahoma");
//        cellStyle.setFont(font);
//        return cellStyle;
//    }
//
//    public XSSFCellStyle getNameCellstyle(){
//        XSSFCellStyle cellStyle = wb.createCellStyle();
//        cellStyle.setBorderTop(BorderStyle.THIN);//上边框
//        cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
//        cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
//        cellStyle.setBorderRight(BorderStyle.THIN);//右边框
//        cellStyle.setWrapText(true);
//        cellStyle.setVerticalAlignment(cellStyle.getVerticalAlignmentEnum().CENTER);
//        cellStyle.setAlignment(HorizontalAlignment.CENTER);
//        Font font1 = wb.createFont();
//        font1.setBold(true);
//        font1.setFontHeightInPoints((short) 11);
//        font1.setFontName("宋体");
//        cellStyle.setFont(font1);
//        return cellStyle;
//    }

    public void setJiangtai(){
        sheet.createRow(kaoChang.getRowNumber()+1);
        sheet.createRow(kaoChang.getRowNumber()+2);
        sheet.createRow(kaoChang.getRowNumber()+3);
        for(int i=0;i<4;i++){
            XSSFCell cell=sheet.getRow(kaoChang.getRowNumber()+2).createCell((int)(Math.ceil(kaoChang.getColNumber()*3/2)-2-1+i));
            XSSFCellStyle cellStyle = wb.createCellStyle();
            cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框
            cellStyle.setBorderBottom(BorderStyle.MEDIUM);
            cellStyle.setBorderLeft(BorderStyle.MEDIUM);
            cellStyle.setBorderRight(BorderStyle.MEDIUM);
            cell.setCellStyle(cellStyle);
            cell=sheet.getRow(kaoChang.getRowNumber()+3).createCell((int)(Math.ceil(kaoChang.getColNumber()*3/2)-2-1+i));
            cellStyle = wb.createCellStyle();
            cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框
            cellStyle.setBorderBottom(BorderStyle.MEDIUM);
            cellStyle.setBorderLeft(BorderStyle.MEDIUM);
            cellStyle.setBorderRight(BorderStyle.MEDIUM);
            cell.setCellStyle(cellStyle);
        }
        CellRangeAddress range=new CellRangeAddress(kaoChang.getRowNumber()+2,kaoChang.getRowNumber()+3,(int)(Math.ceil(kaoChang.getColNumber()*3/2)-1-2),(int)(Math.ceil(kaoChang.getColNumber()*3/2)-1+1));
        sheet.addMergedRegion(range);
        XSSFCell cell=sheet.getRow(kaoChang.getRowNumber()+2).getCell((int)(Math.ceil(kaoChang.getColNumber()*3/2)-2-1));
        cell.setCellValue("讲  台");
        Font font = wb.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 16);
        font.setFontName("宋体");
        cell.getCellStyle().setFont(font);
        cell.getCellStyle().setVerticalAlignment(cell.getCellStyle().getVerticalAlignmentEnum().CENTER);
        cell.getCellStyle().setAlignment(HorizontalAlignment.CENTER);
    }

    public void setDoor(){
        String sheetName=sheet.getSheetName();
        XSSFCell cell;
        if(kaoChang.getDoor().equals("左")){
            cell=sheet.getRow(kaoChang.getRowNumber()+2).createCell(0);
        }else{
            cell=sheet.getRow(kaoChang.getRowNumber()+2).createCell(kaoChang.getColNumber()*3-2-1);
        }
        XSSFCellStyle cellStyle = wb.createCellStyle();
        cell.setCellValue("正 门");
        Font font=wb.createFont();
        font.setBold(true);
        font.setFontName("宋体");
        cellStyle.setFont(font);
        cellStyle.setVerticalAlignment(cell.getCellStyle().getVerticalAlignmentEnum().CENTER);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框
        cellStyle.setBorderBottom(BorderStyle.MEDIUM);
        cellStyle.setBorderLeft(BorderStyle.MEDIUM);
        cellStyle.setBorderRight(BorderStyle.MEDIUM);
        cell.setCellStyle(cellStyle);
    }

    public void setRange(){
        for(int c=0;c<kaoChang.getColNumber()-1;c++){
            CellRangeAddress range=new CellRangeAddress(1,kaoChang.getRowNumber(),c*3+2,c*3+2);
            sheet.addMergedRegion(range);
            XSSFCell cell=sheet.getRow(1).getCell(c*3+2);
            XSSFCellStyle cellStyle = wb.createCellStyle();
            cellStyle.setBorderTop(BorderStyle.THIN);//上边框
            cell.setCellStyle(cellStyle);
            cell=sheet.getRow(kaoChang.getRowNumber()).getCell(c*3+2);
            cellStyle = wb.createCellStyle();
            cellStyle.setBorderBottom(BorderStyle.THIN);//上边框
            cell.setCellStyle(cellStyle);
        }
        for(int c=0;c<kaoChang.getColNumber();c++){
            sheet.setColumnWidth(c*3,14*256);
            sheet.setColumnWidth(c*3+1,2*256*2);
            if(c<6-1){
                sheet.setColumnWidth(c*3+2,(int)(1.5*256*2));
            }
        }
    }

    public List<Map<String,Object>> setNameAndNumber(){
        int index=0;
        List<Map<String,Object>> result=new ArrayList<>();
        for(int r=0;r<kaoChang.getRowNumber();r++){
            XSSFRow row=sheet.createRow(r+1);
            row.setHeight(Const.nameAndNumberCellHeight);
            for(int c=0;c<kaoChang.getColNumber();c++){
                index=c*kaoChang.getRowNumber()+r;
                XSSFCell cell=row.createCell(c*3);
                if(kaoChang.getDatas()!=null&&kaoChang.getDatas().size()>index){
                    kaoChang.getDatas().get(index).put("考场",sheet.getSheetName());
                    kaoChang.getDatas().get(index).put("座位号",index+1);
                    result.add(kaoChang.getDatas().get(index));
                    cell.setCellValue((String)kaoChang.getDatas().get(index).get("考生号"));
                }else{
                    cell.setCellValue("");
                }
                cell.setCellStyle(getCellStyle(true,true,false,Const.kaohaoPointBumber));

                XSSFCell cell1=row.createCell(c*3+1);
                if(kaoChang.getDatas()!=null&&kaoChang.getDatas().size()>index){
                    cell1.setCellValue((String)kaoChang.getDatas().get(index).get("姓名"));

                }else{
                    cell1.setCellValue("");
                }
                cell1.setCellStyle(getCellStyle(true,true,true,Const.namePointNumber));

                row.createCell(c*3+2);

            }
        }
        return result;
    }

    public void setBootom(){
        XSSFRow row=sheet.createRow(kaoChang.getRowNumber()+4+1);
        for(int i=0;i<kaoChang.getColNumber()*3-1;i++){
            row.createCell(i);
        }
        CellRangeAddress range=new CellRangeAddress(kaoChang.getRowNumber()+4+1,kaoChang.getRowNumber()+4+1,0,kaoChang.getColNumber()*3-1);
        sheet.addMergedRegion(range);
        XSSFCell cell=row.getCell(0);
        XSSFCellStyle cellStyle = wb.createCellStyle();
        cell.setCellValue(kaoChang.getName()+" "+kaoChang.getAddress()+" "+kaoChang.getSize());
        Font font = wb.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 20);
        font.setFontName("宋体");
        cellStyle.setFont(font);
        cellStyle.setVerticalAlignment(cell.getCellStyle().getVerticalAlignmentEnum().CENTER);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cell.setCellStyle(cellStyle);
    }

    public List<Map<String,Object>> creatSheet(String sheetName, String sheetTitle){
        try{
            this.sheet=wb.createSheet(sheetName);
            setTitle(sheetTitle,kaoChang.getColNumber()*3-2);
            List<Map<String,Object>> result= setNameAndNumber();
            setRange();
            setJiangtai();
            setDoor();
            setBootom();
            System.out.println("数据量:"+kaoChang.getDatas().size());
            kaoChang.getDatas().removeAll(result);
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            wb.write(fileOutputStream);
//            wb.close();//为啥不可以关闭？
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    public void createResultSheet(List<Map<String,Object>> datas,String sheetName,String sheetTitle){
        try{
            this.sheet = wb.createSheet(sheetName);
            setTitle(sheetTitle,4);
            setResultSheetFlag();
            setResultSheetData(datas);
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            wb.write(fileOutputStream);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void setResultSheetFlag(){
        XSSFRow row=sheet.createRow(1);
        String[] names=new String[]{"高二学号","考生号","姓名","考场","座位号"};
        for(int i=0;i<5;i++){
            XSSFCell cell=row.createCell(i);
            cell.setCellValue(names[i]);
            XSSFCellStyle cellStyle=wb.createCellStyle();
            cellStyle.setVerticalAlignment(cellStyle.getVerticalAlignmentEnum().CENTER);
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            Font font = wb.createFont();
            font.setBold(true);
            font.setFontName("宋体");
            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);
            sheet.setColumnWidth(i,13*256);
        }
    }

    public void setResultSheetData(List<Map<String,Object>> datas){
        Iterator<Map<String,Object>> iterator=datas.iterator();
        String[] names=new String[]{"高二学号","考生号","姓名","考场","座位号"};
        int index=2;
        while(iterator.hasNext()){
            Map<String,Object> data=iterator.next();
            XSSFRow row=sheet.createRow(index);
            index++;
            for(int i=0;i<5;i++){
                XSSFCell cell=row.createCell(i);
                cell.setCellValue(data.get(names[i]).toString());
                XSSFCellStyle cellStyle=wb.createCellStyle();
                cellStyle.setVerticalAlignment(cellStyle.getVerticalAlignmentEnum().CENTER);
                cellStyle.setAlignment(HorizontalAlignment.CENTER);
                cell.setCellStyle(cellStyle);
            }
        }
    }

    public void creatModel(int r,int c){
        for(int i=0;i<r+6;i++){
            XSSFRow row=sheet.createRow(i);
            for(int j=0;j<c*3-1;j++){
                row.createCell(j);
            }
        }
    }
}
